package com.example.hello_world.Toast;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.drawable.Icon;
import android.media.Image;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hello_world.R;

public class ToastStyle extends AppCompatActivity implements View.OnClickListener {

    private Button Btn_Toast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast_style);

        Btn_Toast=findViewById(R.id.button1);
        Btn_Toast.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                Toast toast=Toast.makeText(this, "这是好看的自定义Taost", Toast.LENGTH_LONG);

                //自定义toast布局
                LinearLayout layout= (LinearLayout) toast.getView();
                TextView tv=layout.findViewById(android.R.id.message);
                tv.setTextColor(Color.RED);
                tv.setGravity(Gravity.CENTER);
                //设置背景
                //layout.setBackground(getDrawable(R.mipmap.zz01));
                layout.setBackgroundColor(Color.GREEN);

                //添加图片
                ImageView imageView=new ImageView(this);
                imageView.setImageResource(R.mipmap.zz01);
                layout.addView(imageView,0);

                toast.show();
                break;
        }
    }
}